package com.nxmu.interceptor;

import com.nxmu.entity.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;

/**
 * 检查用户是否登陆
 */
public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        User user = (User) ServletActionContext.getContext().getSession().get("user");
        String requestURI = ServletActionContext.getRequest().getRequestURI();
        //当到注册页面时不需要检查是否登陆
        if (requestURI.contains("register")){
            return actionInvocation.invoke();
        }
        //当获取session中的user为空时，说明用户没有登陆，则转向登陆页面
        // 在登陆时将user放入session中
        if (user == null) return "login";
        return actionInvocation.invoke();
    }
}
